
function formatObj(obj){
    let str = '';    
    for (const key in obj) {
       str += `&${key}=${obj[key]}`
    }
    str = str.slice(1)       
    return str;
}


let http = {
    get:function(url,data,call){
        let xhr = new XMLHttpRequest();
        xhr.open('get',url+'?'+formatObj(data));                
        xhr.send();
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4 && xhr.status == 200){
                let res = JSON.parse(xhr.responseText);          
                call(res);
            }
        }
    },

    post:function(url,data,call){
        let xhr = new XMLHttpRequest();
        xhr.open('post',url);                
        xhr.setRequestHeader('Content-Type', 'application/json')
        xhr.send(JSON.stringify(data));
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4 && xhr.status == 200){
                let res = JSON.parse(xhr.responseText);       
                call(res);
            }
        }
    },

    ajxs:function(obj){
        if(obj.type == 'get'){
            this.get(obj.url,obj.data,obj.sucess);
        }else if(obj.type == 'post'){
            this.post(obj.url,obj.data,obj.sucess)
        }
    }

}